<template>
	<view>
		<view class="image-container">
			<image style="width: 100%;" mode="widthFix" :src="appoint_detail"></image>
		</view>
		<view class="bottom-spacer"></view>
		<view class="button-container">
			<button class="bottom-bar" @click="goToAppoint()">立即预约</button>
		</view>
		<!-- 如果为新用户则弹出 -->
		<view class="newuser-mask" v-if="isNewUser" @tap="closeNewUserDialog"></view>
		<view class="newuser-dialog" v-if="isNewUser">
			<view class="dialog-header">
				<text class="dialog-title">完善个人信息</text>
			</view>
			<view class="dialog-content">
				<view class="form-item">
					<!-- <text class="form-label">姓名</text> -->
					<input class="input-nickname" v-model="userForm.nickName" placeholder="请输入姓名" />
				</view>
				<view class="form-item">
					<!-- <text class="form-label">生日</text> -->
					<picker class="input-birthday" style=" flex: 1;" v-model="userForm.birthday" mode='date'
						:start="minDate" :end="maxDate" @change="setBirthDay">
						<view :style="!userForm.birthday?'color:#8a8383;':''">
							{{userForm.birthday?userForm.birthday:'选择生日'}}
						</view>
					</picker>
				</view>
			</view>
			<view class="dialog-footer">
				<button class="dialog-btn primary-btn" @tap="submitUserInfo">提交</button>
				<button class="dialog-btn cancel-btn" @tap="closeNewUserDialog">稍后</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			// 计算最小和最大出生日期范围
			const now = new Date();
			const maxDate =
				`${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
			const minDate = `${now.getFullYear() - 100}-01-01`;
			return {
				appoint_detail: '',
				isNewUser: false,
				userInfo: null,
				userForm: {
					nickName: '',
					birthday: ''
				},
				minDate,
				maxDate
			}
		},
		onLoad(options) {
			this.goodsIds = options.goodsIds
			if (this.goodsIds) {
				this.fetchGoodsDetail()
			}
			const that = this
			that.$http.GET('/app/order/order/getImageUrl', {}, res => {
				console.log(res)
				that.appoint_detail = res.data.appoint_detail
			})
		},
		methods: {
			goToAppoint() {
				if (uni.getStorageSync('token') == null) {
					this.$tools.routerTo('/pages/index/login', 'navigateTo', 'none')
					return;
				} else {
					const that = this;
					this.$http.GET('/app/user/info/person', {}, function(res) {
						that.userInfo = res.data;
						// 在回调中检查用户信息并跳转
						if (that.userInfo.nickName == '新用户') {
							that.isNewUser = true;
						} else {
							that.$tools.routerTo('/pages/index/appointWeek', 'navigateTo', 'none');
						}
					})
				}

			},
			// 设置生日
			setBirthDay(e) {
				const value = e.detail.value.split('-');
				this.userForm.birthday = `${value[0]}-${value[1].padStart(2, '0')}-${value[2].padStart(2, '0')}`;
			},
			// 关闭新用户弹窗
			closeNewUserDialog() {
				this.isNewUser = false;
			},
			// 设置生日
			setBirthDay(e) {
				const value = e.detail.value.split('-');
				this.userForm.birthday = `${value[0]}-${value[1].padStart(2, '0')}-${value[2].padStart(2, '0')}`;
			},
			// 提交用户信息
			submitUserInfo() {
				// 验证姓名
				if (!this.userForm.nickName.trim()) {
					uni.showToast({
						title: '请输入姓名',
						icon: 'none'
					});
					return;
				}

				// 验证生日
				if (!this.userForm.birthday) {
					uni.showToast({
						title: '请选择生日',
						icon: 'none'
					});
					return;
				}

				// 提交
				this.$http.POST('/app/user/info/updatePerson', this.userForm, res => {
					console.log(res.message)
					if (res.message == "success") {
						console.log('成功保存用户信息', res);
						uni.showToast({
							title: '信息保存成功',
							icon: 'success'
						});
					}
					// 关闭弹窗
					this.closeNewUserDialog();
				});
			}
		}
	}
</script>

<style>
	/* 新用户弹窗样式 */
	.newuser-mask {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		z-index: 999;
	}

	.newuser-dialog {
		position: fixed;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		width: 600rpx;
		background-color: #fff;
		border-radius: 20rpx;
		z-index: 1000;
		overflow: hidden;
	}

	.dialog-header {
		padding: 30rpx;
		text-align: center;
		border-bottom: 1rpx solid #e0e0e0;
	}

	.dialog-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #089790;
	}

	.dialog-content {
		padding: 40rpx 30rpx;
	}

	.form-item {
		margin-bottom: 30rpx;
	}

	.form-label {
		font-size: 28rpx;
		color: #333;
		margin-bottom: 10rpx;
		display: block;
	}

	.input-nickname {
		width: 100%;
		height: 80rpx;
		border: 1rpx solid #ddd;
		border-radius: 8rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
		box-sizing: border-box;
	}

	.input-birthday {
		width: 100%;
		height: 80rpx;
		border: 1rpx solid #ddd;
		border-radius: 8rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.form-input {
		width: 100%;
		height: 80rpx;
		border: 1rpx solid #ddd;
		border-radius: 8rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
		box-sizing: border-box;
	}

	.dialog-message {
		font-size: 28rpx;
		color: #333;
		line-height: 1.5;
	}

	.dialog-footer {
		display: flex;
		padding: 20rpx 30rpx 30rpx;
	}

	.dialog-btn {
		flex: 1;
		height: 80rpx;
		line-height: 80rpx;
		border-radius: 40rpx;
		font-size: 32rpx;
		margin: 0 10rpx;
	}

	.primary-btn {
		background-color: #089790;
		color: #fff;
	}

	.cancel-btn {
		background-color: #f8f8f8;
		color: #666;
	}

	.image-container {
		padding-bottom: 10px;
	}

	.bottom-spacer {
		height: 80px;
		/* 添加足够的高度，确保底部按钮不会遮挡图片 */
	}

	.button-container {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 20px;
	}

	.bottom-bar {
		width: 100%;
		height: 60px;
		display: flex;
		justify-content: center;
		align-items: center;
		background: linear-gradient(180deg, #FFF1B3 0%, #FFE17A 100%);
		border-radius: 30px;
		text-align: center;
		color: #089790;
		font-size: 18px;
		font-weight: bold;
		border: none;
		box-shadow: none;
		box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
	}

	.price {
		font-size: 18px;
		color: #e64340;
	}

	.purchase-btn {
		background-color: #e64340;
		color: white;
		border-radius: 20px;
		width: 120px;
		margin: 0;
	}
</style>